package com.shanzmoo.db.order.entity.vo;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.shanzmoo.db.personnel.entity.MemberInfoEntity;
import com.shanzmoo.db.order.entity.OutletOrderEntity;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 门店订单分页返回对象
 * @author: Gzhao 2020/12/14
 */
@Data
@ApiModel("门店订单分页返回对象")
public class OutletOrderPageVo {

    @ApiModelProperty("订单Id")
    private Integer orderId;

    @ApiModelProperty("订单号")
    private String orderNo;

    @ApiModelProperty("客户ID")
    private Integer memberId;

    @ApiModelProperty("客户名称")
    private String memberName;

    @ApiModelProperty("是否刷机订单: false-否，true-是")
    private Boolean root;

    @ApiModelProperty("订单状态:0-新开单，1-刷机中，2-待取货，3-已完结，4-已结算, 9-已作废")
    private Integer status;

    @ApiModelProperty("支付状态: false-未支付，true-已支付")
    private Boolean payStatus;

    @ApiModelProperty("发货类型：0-自提，1-物流")
    private Integer deliveryType;

    @JsonFormat(pattern="yyyy-MM-dd")
    @ApiModelProperty("预计完成时间")
    private LocalDate expectFinishDate;

    @ApiModelProperty("实收金额（单位元）")
    private BigDecimal receiveAmount;

    @ApiModelProperty("收件人")
    private String consignee;

    @ApiModelProperty("收件人手机")
    private String consigneeMobile;

    @JsonFormat(pattern="yyyy-MM-dd HH:mm")
    @ApiModelProperty("创建时间")
    private LocalDateTime createTime;

    public static OutletOrderPageVo create(OutletOrderEntity outletOrder, List<MemberInfoEntity> memberList) {
        OutletOrderPageVo pageVo = JSON.parseObject(JSON.toJSONString(outletOrder), OutletOrderPageVo.class);

        pageVo.setOrderId(outletOrder.getId());
        List<MemberInfoEntity> list = memberList.stream()
                .filter(e -> Objects.equals(e.getId(), outletOrder.getMemberId()))
                .collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(list)){
            pageVo.setMemberName(list.get(0).getName());
        }
        return pageVo;
    }
}
